package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ChannelGroupUserAddDTO;
import org.jsola.hr.dto.ChannelGroupUserUpdateDTO;
import org.jsola.hr.query.ChannelGroupUserQuery;
import org.jsola.hr.service.IChannelGroupUserService;
import org.jsola.hr.vo.ChannelGroupUserListVO;
import org.jsola.hr.vo.ChannelGroupUserVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 渠道分组用户中间表
 *
 * @author zhr
 */
@RestController("hrChannelGroupUserController")
@RequestMapping(HrConstants.API_PATH + "/v1/channelGroupUsers")
public class ChannelGroupUserController implements IChannelGroupUserControllerAPI {

    @Autowired
    private IChannelGroupUserService channelGroupUserService;

    @Override
    @DeleteMapping("/deleteGroupManager")
    public Result<Integer> deleteGroupManager(@Valid @RequestBody ChannelGroupUserQuery channelGroupUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                channelGroupUserService.deleteGroupManager(channelGroupUserQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/batchSave")
    public Result<Integer> batchSave(@Valid @RequestBody ChannelGroupUserAddDTO channelGroupUserAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                channelGroupUserService.batchSave(channelGroupUserAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/getUserByGroupId")
    public Result<List<ChannelGroupUserListVO>> getUserByGroupId(@Valid ChannelGroupUserQuery channelGroupUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增渠道分组用户中间表权限校验

        return Result.success(
                channelGroupUserService.getUserByGroupId(channelGroupUserQuery, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<ChannelGroupUserVO> save(@Valid @RequestBody ChannelGroupUserAddDTO channelGroupUserAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增渠道分组用户中间表权限校验

        return Result.success(
                channelGroupUserService.save(channelGroupUserAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ChannelGroupUserUpdateDTO channelGroupUserUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改渠道分组用户中间表权限校验

        int count = channelGroupUserService.update(channelGroupUserUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{channelGroupUserId}")
    public Result<Integer> delete(@PathVariable String channelGroupUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除渠道分组用户中间表权限校验

        int count = channelGroupUserService.deleteByIds(tokenUser, channelGroupUserId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] channelGroupUserIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除渠道分组用户中间表权限校验

        int count = channelGroupUserService.deleteByIds(tokenUser, channelGroupUserIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{channelGroupUserId}")
    public Result<ChannelGroupUserVO> findById(@PathVariable String channelGroupUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看渠道分组用户中间表权限校验

        return Result.success(
            channelGroupUserService.selectById(channelGroupUserId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ChannelGroupUserListVO>> listAll(@Valid ChannelGroupUserQuery channelGroupUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看渠道分组用户中间表权限校验

        return Result.success(
            channelGroupUserService.select(channelGroupUserQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ChannelGroupUserListVO>> page(@Valid ChannelGroupUserQuery channelGroupUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            channelGroupUserService.selectPage(channelGroupUserQuery, tokenUser.getSiteId())
        );
    }


}

